<?php
defined('IN_IXDCW') or exit('Access Denied');
/**
 * 摇奖获得奖项记录日志
 * @author  Sameal
 */
class ErnieModule extends BasicModule {

	/** @var 模块id */
	private $_moduleid;

	/** @var 条目ID */
	private $_id;

	/** @var string 表格 */
	private $_table;

	/** @var string 表格数据 */
	private $_tableData;

	/** @var 是否内容分表 */
	private $_split;

	/** @var array 可用字段 */
	private $_fields;


	/**
	 * constructor
	 * @param $moduleid
	 */
	function __construct() {
		parent::__construct();
		global $prize_data, $MOD;
		$this->_table     = $prize_data;
		$this->_split     = $MOD['split'];
		$this->_fields = array('id','userid','username','prizeid','prizename','datetime','time','ip');
    }
	public function setid($id) {
		$this->_id = $id;
	}

	public function getid() {
		return $this->_id;
	}

	public function getFields() {
		return $this->_fields;
	}

	/**
	 * 是否通过插入检测
	 * @param $post
	 * @return bool
	 */
	function pass($post) {
		if(!is_array($post)) return false;

		return true;
	}

	/**
	 * 设置符合内容的值
	 * @param $post
	 * @return array|mixed
	 */
	function set($post) {
		global $MOD, $DT_TIME;

		return $post;
	}
	/**
	 * 获取一条数据
	 * @return array|void
	 */
	function getOne($condition) {
		if($condition){
			return $this->_db->getOne("SELECT * FROM {$this->_table}  WHERE {$condition}");
		}else{
			return $this->_db->getOne("SELECT * FROM {$this->_table}  WHERE id=$this->_id");
		}
	}

	/**
	 * 获取列表
	 * @param string $condition
	 * @param string $order
	 * @param string $cache
	 * @return array
	 */
	function getList($condition = 'status=3', $order = 'time DESC', $cache = '') {
		global $items;
		$num = $this->_db->count($this->_table, $condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = $catids = $CATS = array();
		$result = $this->_db->query($sql, $cache);

		while($r = $this->_db->fetchArray($result)) {
			$lists[] = $r;
		}
		return $lists;
	}

	/**
	 * 添加数据
	 * @param $post
	 */
	function add($post) {
		global $MOD,$_userid;
		$post = $this->set($post);
		$this->_db->query(Sql::insert($this->_table, $post));
		$this->_id = $this->_db->insertId();
		return $this->_id;
	}


	/**
	 * 回收站
	 * @param $id
	 * @return bool
	 */
	function recycle($id) {
		if(is_array($id)) {
			foreach($id as $v) { $this->recycle($v); }
		} else {
			$this->_db->query(Sql::update($this->_table,"status=0","id=$id"));
			$this->delete($id, false);
			return true;
		}		
	}

	/**
	 * 还原
	 * @param $id
	 * @return bool
	 */
	function restore($id) {
		global $module, $MOD;
		if(is_array($id)) {
			foreach($id as $v) { $this->restore($v); }
		} else {
			$this->_db->query(Sql::update($this->_table,"status=3","id=$id"));
			return true;
		}		
	}

	/**
	 * 删除条目
	 * @param      $id
	 * @param bool $all
	 */
	function delete($id, $all = true) {
		if(is_array($id)) {
			foreach($id as $v) {
				$this->delete($v, $all);
			}
		} else {
			$this->_id = $id;
			$r = $this->getOne('');
			if($all) {
				$this->_db->query(Sql::delete($this->_table,"id=$id"));
			}
		}
	}

	/**
	 * 清空回收站内容
	 * @param string $condition
	 */
	function clear($condition = 'status=0') {		
		$result = $this->_db->query("SELECT id FROM {$this->_table} WHERE $condition ");
		while($r = $this->_db->fetchArray($result)) {
			$this->delete($r['id']);
		}
	}

}
